<template>
  <div class="zy-content ">
    <div class="editSparebox">
      <div class="box_list">
        <div class="list_item">
          <span class="left">
            {{$t('maintenance.SparePart')}}
          </span>
          <span class="right">
            <van-field disabled v-model="listObj.materielNo"  placeholder="请点击图标进行选择备件"/>
            <van-icon class="icon" name="arrow" @click="addDeviceno('料号')"/>
          </span>
        </div>
        <div class="list_item">
          <span class="left">
             {{$t('maintenance.SpareNumber')}}
          </span>
          <span class="right">
            <van-field disabled v-model="listObj.materialCode" placeholder="请输入或选择"/>
            <!--            <van-icon class="icon" name="arrow" @click="addDeviceno('编号')"/>-->
          </span>
        </div>
        <div class="list_item">
          <span class="left">
            {{$t('maintenance.SparePartsName')}}
          </span>
          <span class="right">
            <van-field disabled v-model="listObj.goodsName" placeholder="请输入"/>
          </span>
        </div>
        <div class="list_item">
          <span class="left">
            {{$t('maintenance.Specification')}}
          </span>
          <span class="right">
            <van-field disabled v-model="listObj.specification" placeholder="请输入"/>
          </span>
        </div>
        <div class="list_item">
          <span class="left">
             {{$t('maintenance.Amount')}}
          </span>
          <span class="right">
            <van-field :disabled="singleStatus" v-model="listObj.num" placeholder="请输入"/>
          </span>
        </div>
        <div class="list_item">
          <span class="left">
        {{$t('maintenance.unitprice')}}
          </span>
          <span class="right">
            <van-field disabled v-model="listObj.unitPrice" placeholder="请输入"/>
          </span>
        </div>
      </div>

      <div class="editBtn">
        <span class="saveBtn" @click="saveMatence">{{$t('maintenance.Enter')}}</span>
        <span class="cancleBtn" @click="$router.go(-1)">{{$t('maintenance.Cancle')}}</span>
      </div>
    </div>
    <Modal :mask-closable="false"
          v-model="popupShow"
           footer-hide
          :title="titleText"
           >
                <addspareinfo
                    ref="addspareinfo"
                    :titleText="titleText"
                    @getListobj="getListobj"
                    @changeStatus="popupChange"/>
    </Modal>
  </div>
</template>

<script>
import { saveMaintence } from "@/api/maintenance";
import { mapGetters } from "vuex";
import addspareinfo from "./addspareinfo";
export default {
  name: "editspareinfo",
  components:{addspareinfo},
  computed: {
    ...mapGetters([
      'shareList'
    ])
  },
  data(){
    return{
      singleStatus:false,
      textTitle:'添加备件',
      listObj:{
        materielNo:"",
        materialCode:"",
        goodsName:"",
        specification:"",
        num:"",
        unitPrice:"",
      },
      titleText:"",
      popupShow:false,

    }
  },
  mounted() {
    if (this.$route.query.status === 'edit'){
      this.textTitle = '编辑备件'
    }
    const obj = this.shareList
    if (obj){
       this.getListobj(obj)
    }
  },
  beforeDestroy() {
    localStorage.clear('shareEdit')
  },
  methods:{
    popupClose(){
      this.$refs.addspareinfo.clearListinfo()
    },
    saveMatence(){
      const data = {
        materielNo:this.listObj.materielNo,
        materialCode:this.listObj.materialCode,
        goodsName:this.listObj.goodsName,
        specification:this.listObj.specification,
        num:this.listObj.num,
        unitPrice:this.listObj.unitPrice
      }
      if (this.$route.query.status !== 'edit'){
        data.itemId = this.$route.query.itemId
        data.taskId = this.$route.query.taskId
        data.useOutInfoId = this.listObj.id
      }else {
        data.useOutInfoId = this.listObj.useOutInfoId
        data.id = this.listObj.id
      }
      saveMaintence(data,this.$route.query.status).then(res=>{
        if (res.code == 200){
          const query = {
            itemId:this.$route.query.itemId,
            taskId:this.$route.query.taskId,
            orgId:this.$route.query.orgId
          }
          this.$router.replace({
            path:"/addspare",
            query
          })
          this.$router.go(-1)
        }else {
          this.$Message.warning(res.msg)
        }
      })
    },
    getListobj(data){
      if (data.manageType === 1){
        this.singleStatus = true
        data.num = 1
      }
      this.listObj = {
        materielNo:data.materielNo,
        materialCode:data.purchaseOrderNo,
        num:data.outNum,
        unitPrice:data.purchasePrice,
        ...data
      }
    },
    popupChange(val){
      this.popupShow = val
    },
    addDeviceno(str){
      this.popupShow = false
      if (this.$route.query.status === 'edit'){
        return
      }
      switch (str) {
       case '料号':
         this.titleText = '备件料号';
         break;
        case '编号':
          this.titleText = '备件编号';
          break;
      }
      this.popupShow = true
    }
  }
}
</script>

<style scoped lang="scss">
@import "index.scss";
::v-deep .ivu-modal-body{
  padding: 0;
}
</style>
